﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.MEP
{
	public class HyProjectFamily
	{
		public static List<Family> GetFamilys(BuiltInCategory category, Document doc)
		{
			IList<Element> list = new FilteredElementCollector(doc).OfClass(typeof(Family)).ToElements();
			if (list.Count < 1)
			{
				return null;
			}
			List<Family> list2 = new List<Family>();
			foreach (Element element in list)
			{
				Family family = (Family)element;
				if (family.FamilyCategory.Id.IntegerValue == (int)category)
				{
					list2.Add(family);
				}
			}
			return list2;
		}

		public static void BingLvwFamilys(List<Family> familys, ListView lvwPrj, Document doc)
		{
			lvwPrj.Items.Clear();
			if (familys == null)
			{
				return;
			}
			if (familys.Count < 1)
			{
				return;
			}
			foreach (Family family in familys)
			{
				ListViewItem value = new ListViewItem(family.Name)
				{
					Tag = family
				};
				lvwPrj.Items.Add(value);
			}
		}

		public static void BingTvwFamilysTwoLevel(List<Family> familys, TreeView tvwCurrPrj)
		{
			tvwCurrPrj.Nodes[0].Nodes.Clear();
			TreeNode treeNode = tvwCurrPrj.Nodes[0];
			if (familys == null)
			{
				return;
			}
			if (familys.Count < 1)
			{
				return;
			}
			foreach (Family family in familys)
			{
				TreeNode node = new TreeNode(family.Name)
				{
					Tag = family
				};
				treeNode.Nodes.Add(node);
			}
			tvwCurrPrj.ExpandAll();
		}

		public static void BingTvwFamilys(List<Family> familys, TreeView tvwCurrPrj)
		{
			tvwCurrPrj.Nodes[0].Nodes.Clear();
			TreeNode treeNode = tvwCurrPrj.Nodes[0];
			if (familys == null)
			{
				return;
			}
			if (familys.Count < 1)
			{
				return;
			}
			foreach (Family family in familys)
			{
				TreeNode treeNode2 = new TreeNode(family.Name)
				{
					Tag = family
				};
				HyProjectFamily.BingTvwFamilySymbol(treeNode2, family);
				treeNode.Nodes.Add(treeNode2);
			}
			tvwCurrPrj.SelectedNode = treeNode;
			tvwCurrPrj.ExpandAll();
		}

		private static void BingTvwFamilySymbol(TreeNode tnFamily, Family family)
		{
			foreach (FamilySymbol familySymbol in family.GetSymbols())
			{
				if (familySymbol != null)
				{
					TreeNode node = new TreeNode(familySymbol.Name)
					{
						Tag = familySymbol
					};
					tnFamily.Nodes.Add(node);
				}
			}
		}

		public static string CreateImageByFamily(Family family, string folderName, Document doc)
		{
			string text = "";
			if (!File.Exists(Path.Combine(Product.FamilyImageLocation, folderName, family.Name + " - 楼层平面 - 参照标高.png")))
			{
				Document document = doc.EditFamily(family);
				ImageExportOptions imageExportOptions = new ImageExportOptions();
                imageExportOptions.FitDirection = FitDirectionType.Vertical;
                imageExportOptions.HLRandWFViewsFileType = ImageFileType.PNG;
                imageExportOptions.ImageResolution = ImageResolution.DPI_300;
				imageExportOptions.ShouldCreateWebSite = false;
                imageExportOptions.ExportRange = ExportRange.SetOfViews;
				imageExportOptions.ZoomType = 0;
				imageExportOptions.Zoom = 100;
				imageExportOptions.FilePath = Path.Combine(Product.FamilyImageLocation, folderName, family.Name);
				imageExportOptions.PixelSize = 640;
				ElementId elementId = null;
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
                FilteredElementIterator elementIterator = filteredElementCollector.OfClass(typeof(Autodesk.Revit.DB.View)).GetElementIterator();
				elementIterator.Reset();
				while (elementIterator.MoveNext())
				{
					Element element = elementIterator.Current;
                    Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
                    if (view != null && !view.IsTemplate && view.ViewType == (ViewType)1 && view.Name == "参照标高")
					{
						elementId = view.Id;
						break;
					}
				}
				Transaction transaction = new Transaction(document, "deleteLines");
				transaction.Start();
				List<Element> list = filteredElementCollector.OfClass(typeof(ReferencePlane)).ToElements().ToList<Element>();
				List<Element> list2 = filteredElementCollector.OfClass(typeof(Dimension)).ToElements().ToList<Element>();
				foreach (Element elem in list)
				{
					document.DeleteElement(elem);
				}
				foreach (Element elem2 in list2)
				{
					document.DeleteElement(elem2);
				}
				document.Regenerate();
				transaction.Commit();
				if (null != elementId)
				{
					imageExportOptions.SetViewsAndSheets(new List<ElementId>
					{
						elementId
					});
					document.ExportImage(imageExportOptions);
					text = imageExportOptions.FilePath + " - 楼层平面 - 参照标高.png";
				}
			}
			else
			{
				text = Path.Combine(Product.FamilyImageLocation, folderName, family.Name + " - 楼层平面 - 参照标高.png");
			}
			if (!File.Exists(text))
			{
				text = Path.Combine(Product.FamilyImageLocation, "default.bmp");
			}
			return text;
		}

		public static string GetImageByFamily(Family family, string folderName, Document doc)
		{
			string result;
			if (File.Exists(Path.Combine(Product.FamilyImageLocation, folderName, family.Name + ".png")))
			{
				result = Path.Combine(Product.FamilyImageLocation, folderName, family.Name + ".png");
			}
			else
			{
				result = HyProjectFamily.CreateImageByFamily(family, folderName, doc);
			}
			return result;
		}
	}
}
